package com.light.gulimall.product.controller;

import com.light.common.utils.PageUtils;
import com.light.common.utils.R;
import com.light.gulimall.product.dto.AttrDTO;
import com.light.gulimall.product.dto.UpdateSpuDTO;
import com.light.gulimall.product.entity.ProductAttrValueEntity;
import com.light.gulimall.product.feign.CouponRemoteServiceClient;
import com.light.gulimall.product.service.AttrService;
import com.light.gulimall.product.vo.AttrVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author minmin
 * @email 980680541@gmail.com
 * @date 2024-07-16 19:47:27
 */
@Slf4j
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private CouponRemoteServiceClient couponRemoteServiceClient;

    @RequestMapping("/test")
    public void test() {
        try {
            couponRemoteServiceClient.testCouponRemote();
        } catch (Exception e) {
            log.error("报错:", e);
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
//		AttrEntity attr = attrService.getById(attrId);
        AttrVO vo = attrService.getInfoById(attrId);
        return R.ok().put("attr", vo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrDTO attr) {
        attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrDTO dto) {
//		attrService.updateById(attr);
        attrService.modifyById(dto);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

    /**
     * 获取分类规格参数
     *
     * @param params    查询条件
     * @param catelogId 所属分类id,pms_category 主键id
     * @return 分页查询结果
     */
    @RequestMapping("/base/list/{catelogId}")
    public R getBaseList(@RequestParam Map<String, Object> params,
                         @PathVariable("catelogId") Long catelogId) {
        return R.ok().put("page", attrService.getBaseList(params, catelogId, 1L));
    }


    /**
     * 获取分类销售属性
     *
     * @param catelogId 所属分类id,pms_category 主键id
     * @return 分页查询结果
     */
    @RequestMapping("/sale/list/{catelogId}")
    public R getCatelogList(@RequestParam Map<String, Object> params,
                            @PathVariable("catelogId") Long catelogId) {
        try {
            return R.ok().put("page", attrService.getBaseList(params, catelogId, 0L));
        } catch (Exception e) {
            log.error("发生异常,", e);
        }
        return null;
    }

    /**
     * 修改商品规格
     * @param spuId spu主键id
     * @param dto   前端传递信息
     * @return 是否修改成功
     */
    @PostMapping("/update/{spuId}")
    public R updateSpu(@PathVariable("spuId") Long spuId, @RequestBody List<UpdateSpuDTO> dto) {
        attrService.updateSpu(spuId, dto);
        return R.ok();
    }

    /**
     * 获取spu规格
     * @param spuId spu主键id
     * @return 查询结果
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R getBaseListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> data = attrService.getBaseListForSpu(spuId);
        return R.ok().put("data", data);
    }
}
